/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml.timgiayto.binding.info;

import java.io.File;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import xml.timgiayto.utils.CommonUtils;
import xml.timgiayto.utils.DBUtils;

/**
 *
 * @author mrnghiep
 */
public class InfoJAXBProcessor {

    /**
     * Function to get list information from database.
     * @return List<Informations>
     */
    public static List<Informations> getInformations() {
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            // Make connection
            con = DBUtils.makeConnection();

            // Call procedure getListInfo from SQL
            call = con.prepareCall("{call getListInfo()}");
            rs = call.executeQuery();

            Informations informations = new Informations();
            Informations informationsConverted = new Informations();

            // Declare List to store informations
            List<Informations> list = new ArrayList<Informations>();

            // Declare attribute of information object
            int id = 0;
            String fullName = "";
            String address = "";
            String phone = "";
            String email = "";
            String info = "";
            String type = "";
            String location = "";
            String owner = "";
            String imageUrl = "";
            String description = "";
            String date = "";
            String category = "";

            while (rs.next()) {
                id = rs.getInt("id");
                fullName = rs.getString("fullName");
                address = rs.getString("address");
                phone = rs.getString("phone");
                email = rs.getString("email");
                info = rs.getString("info");
                type = rs.getString("type");
                location = rs.getString("location");
                owner = rs.getString("owner");
                imageUrl = rs.getString("imageUrl");
                description = rs.getString("description");

                date = rs.getString("date");
                category = rs.getString("category");

                // New object information have Vietnamese from database.
                Information information = new Information(id, fullName, address, phone, email, info, type, location, owner, imageUrl, description, date, category);
                // New object information unsigned Vietnamese from database.
                Information informationConverted = new Information(id, CommonUtils.convertVietnamese(fullName),
                        CommonUtils.convertVietnamese(address), phone, email, CommonUtils.convertVietnamese(info),
                        CommonUtils.convertVietnamese(type), CommonUtils.convertVietnamese(location),
                        CommonUtils.convertVietnamese(owner), imageUrl, CommonUtils.convertVietnamese(description),
                        date, CommonUtils.convertVietnamese(category));

                informations.getInformation().add(information);
                informationsConverted.getInformation().add(informationConverted);
            }
            // Add to list.
            list.add(informations);
            list.add(informationsConverted);
            return list;
        } catch (SQLException e) {
            Logger.getLogger(InfoJAXBProcessor.class.getName()).log(Level.SEVERE, null, e);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (call != null) {
                    call.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                Logger.getLogger(InfoJAXBProcessor.class.getName()).log(Level.SEVERE, null, e);
            }
        }
        return null;
    }
}
